fn parse_input() -> Vec<i32> {
    let input = std::fs::read_to_string("./day07/input.txt").unwrap();
    input.split(',').map(|line| line.parse().unwrap()).collect()
}

#[allow(unused)]
fn part1(numbers: &Vec<i32>) {
    let &max_number = numbers.iter().max().unwrap();
    let &min_number = numbers.iter().min().unwrap();

    let result = (min_number..=max_number).map(|target| -> i32 {
        numbers.iter().map(|&number| (number - target).abs()).sum()
    }).min().unwrap();

    println!("{}", result);
}

#[allow(unused)]
fn part2(numbers: &Vec<i32>) {
    let &max_number = numbers.iter().max().unwrap();
    let &min_number = numbers.iter().min().unwrap();

    fn fuel_cost(begin: i32, end: i32) -> i32 {
        let distance = (begin - end).abs();
        (1 + distance) * distance / 2
    }

    let result = (min_number..=max_number).map(|target| -> i32 {
        numbers.iter().map(|&number| fuel_cost(number, target)).sum()
    }).min().unwrap();

    println!("{}", result);
}

fn main() {
    let numbers = parse_input();
    part1(&numbers);
    part2(&numbers);
}
